package com.ruoyi.system.utils;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO
 * @author: wangp
 * @date: 2025年03月11日 16:15
 */
public class DelayedTask implements Delayed {
    private final Runnable task;
    private final long executeTime;

    public DelayedTask(Runnable task, long delay, TimeUnit unit) {
        this.task = task;
        this.executeTime = System.currentTimeMillis() + unit.toMillis(delay);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(this.executeTime, ((DelayedTask) o).executeTime);
    }

    public void execute() {
        task.run();
    }
}